using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;
using XLua;

//无参无返回值的委托
public delegate void CustomCall();

//有参有返回值的委托
//该特性在xlua中的命名空间中
[CSharpCallLua]
public delegate int CustomCall2(int a, int b);


//多返回值的委托
[CSharpCallLua]
public delegate int CustomCall3(int a, out int b, out bool c, out string d, out int e);

[CSharpCallLua]
public delegate int CustomCall4(int a, ref int b, ref bool c, ref string d, ref int e);

[CSharpCallLua]
public delegate void CustomCall5(string a,params int[] args); // 变长参数的类型根据实际情况而定，但最保险是传object

public class Lesson5_CallFunction : MonoBehaviour
{
    
    void Start()
    {
        LuaMgr.GetInstance().Init();

        LuaMgr.GetInstance().DoFile("Main");

        
        //无参无返回值的获取
        //委托
        //委托可以理解为方法的引用
        CustomCall customCall = LuaMgr.GetInstance().Global.Get<CustomCall>("testFunction");
        customCall();
        

        // Unity中的自带委托
        UnityAction unityAction = LuaMgr.GetInstance().Global.Get<UnityAction>("testFunction");
        unityAction();


        // C#中使用委托
        Action action = LuaMgr.GetInstance().Global.Get<Action>("testFunction");
        action();


        //Xlua中提供的获取Lua方法
        LuaFunction luaFunction = LuaMgr.GetInstance().Global.Get<LuaFunction>("testFunction");
        luaFunction.Call();




        //有参有返回值的获取
        CustomCall2 customCall2 = LuaMgr.GetInstance().Global.Get<CustomCall2>("testFunction2");
        int result = customCall2(1, 2);
        Debug.Log("result = " + result);

        // C#自带的泛型委托
        Func<int,int,int> sFunc = LuaMgr.GetInstance().Global.Get<Func<int,int,int>>("testFunction2");
        Debug.Log("sFunc = " + sFunc(1, 2));

        // Xlua中提供的获取Lua方法
        LuaFunction luaFunction2 = LuaMgr.GetInstance().Global.Get<LuaFunction>("testFunction2");
        Debug.Log("有参有返回值的Xlua方法返回值=" + luaFunction2.Call(1,2)[0]);


        // 多返回值的获取
        //使用Out和ref来接受
        CustomCall3 customCall3 = LuaMgr.GetInstance().Global.Get<CustomCall3>("testFunction3");
        int b;
        bool c;
        string d;
        int e;
        Debug.Log("多返回值的第一个返回值=" + customCall3(1, out b, out c, out d, out e));
        Debug.Log("多返回值的后几个返回值=" + b + "_" + c + "_" + d + "_" + e);
        

        CustomCall4 customCall4 = LuaMgr.GetInstance().Global.Get<CustomCall4>("testFunction3");
        int b1 = 0;
        bool c1 = false;
        string d1 = "";
        int e1 = 0;
        Debug.Log("多返回值的第一个返回值=" + customCall4(100,ref b1,ref c1,ref d1,ref e1));
        Debug.Log("多返回值的后几个返回值=" + b1 + "_" + c1 + "_" + d1 + "_" + e1);

        // Xlua中提供的,好像是最简单的
        LuaFunction luaFunction3 = LuaMgr.GetInstance().Global.Get<LuaFunction>("testFunction3");
        object[] objs = luaFunction3.Call(1000);
        for(int i =0 ;i<objs.Length;i++)
        {
            Debug.Log("多返回值的第" + i + "个返回值=" + objs[i]);
        }



        // 变长参数的获取
        CustomCall5 customCall5 = LuaMgr.GetInstance().Global.Get<CustomCall5>("testFunction4");
        customCall5("变长参数测试",1,2,3,4,5,1000,333);

        // Xlua中提供的
        LuaFunction luaFunction4 = LuaMgr.GetInstance().Global.Get<LuaFunction>("testFunction4");
        luaFunction4.Call("变长参数测试",1,2,3,4,5,1000,333);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
